package com.seguriapp.complaints.gwt.server;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.DispatchException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.restlet.data.MediaType;
import org.restlet.resource.ClientResource;

import com.seguriapp.common.config.GlobalConfigLoader;
import com.seguriapp.common.gwt.server.AbstractSeguriAppHandler;
import com.seguriapp.complaints.gwt.shared.GetComplaintMediaDataAction;
import com.seguriapp.complaints.gwt.shared.GetComplaintMediaDataResult;
import com.seguriapp.entities.MediaContent;
import com.seguriapp.entities.MediaContentType;

public class GetComplaintMediaDataHandler extends AbstractSeguriAppHandler<GetComplaintMediaDataAction, GetComplaintMediaDataResult> {

  private static final Logger LOGGER = Logger.getLogger(GetComplaintMediaDataHandler.class);

  public Class<GetComplaintMediaDataAction> getActionType() {
    return GetComplaintMediaDataAction.class;
  }

  public GetComplaintMediaDataResult execute(final GetComplaintMediaDataAction action, final ExecutionContext context) throws DispatchException {

    final GetComplaintMediaDataResult result = new GetComplaintMediaDataResult();
    try {
      final String serverUrl = GlobalConfigLoader.getInstance().getGlobalConfig().getBackEndUrl();
      final String url = serverUrl.concat(GlobalConfigLoader.getInstance().getGlobalConfig().getGetComplaintMediaContent());
      final ClientResource resource = new ClientResource(String.format(url, action.getComplaintId()));
      final String complaintMediaListJs = resource.get(MediaType.TEXT_PLAIN).getText();
      LOGGER.info("Data received : \n" + complaintMediaListJs);
      result.setMediaDataList(getJsonDataFromString(complaintMediaListJs));
      result.setAudioUrl(serverUrl.concat(GlobalConfigLoader.getInstance().getGlobalConfig().getAudioUrl()));
      result.setImageUrl(serverUrl.concat(GlobalConfigLoader.getInstance().getGlobalConfig().getImageUrl()));
      result.setVideoUrl(serverUrl.concat(GlobalConfigLoader.getInstance().getGlobalConfig().getVideoUrl()));
      result.setAudioValidExt(GlobalConfigLoader.getInstance().getGlobalConfig().getAudioValidExt());
      result.setVideoValidExt(GlobalConfigLoader.getInstance().getGlobalConfig().getVideoValidExt());
    } catch (Exception e) {
      LOGGER.error(e.getStackTrace());
    }
    return result;
  }

  private List<MediaContent> getJsonDataFromString(final String complaintMediaListJs) {
    final List<MediaContent> complaintMediaList = newArrayList();
    final JSONObject root = JSONObject.fromObject(complaintMediaListJs);
    JSONArray dataArray = null;
    JSONObject jo = null;
    if (root.has("mediaContent")) {
      dataArray = root.getJSONArray("mediaContent");
      if (dataArray != null) {
        for (int i = 0; i < dataArray.size(); i++) {
          jo = dataArray.getJSONObject(i);
          final MediaContent mediaContent = processjSONObject(jo);
          complaintMediaList.add(mediaContent);
        }
      }
    }
    return complaintMediaList;
  }

  private MediaContent processjSONObject(final JSONObject jo) {
    if (jo.has("id") && jo.has("type") && jo.has("content")) {
      final MediaContent mediaContent = new MediaContent();
      mediaContent.setId(jo.getLong("id"));
      mediaContent.setType(MediaContentType.getEnumByString(jo.getString("type")));
      mediaContent.setContent(jo.getString("content"));
      return mediaContent;
    }
    return null;
  }

}
